using System;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Test.BO;
using UseCaseManager.UI;

namespace UseCaseManager.Test.UI
{
    /// <summary>
    /// This is a test class for testing the <see cref="ReleasesGridControl"/>
    /// </summary>
    [TestFixture]
    public class TestReleasesGridControl
    {
        private const int _STD_BUTTON_HEIGHT = 33;

        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //Executes once for all tests in this fixture
            //BORegistry.DataAccessor = new DataAccessorDB();

            ClassDef.ClassDefs.Clear();
            ClassDef.LoadClassDefs(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()));
        }

        [SetUp]
        public void TestSetup()
        {
            //Test Setup Runs prior to each test in this fixture executing.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            TestUtils.ClearAllTables();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            TestUtils.WaitForGC();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
        }

        [Test]
        public void Test_CreateReleasesGridControl()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ReleasesGridControl releaseSelector = new ReleasesGridControl();
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseSelector);
            Assert.IsNotNull(releaseSelector.GridSelector);
            Assert.IsInstanceOfType(typeof (ReadOnlyGridControlWin), releaseSelector.GridSelector);
            IButtonGroupControl buttons = releaseSelector.Buttons;
            Assert.IsNotNull(buttons);
            Assert.AreEqual(4, buttons.Controls.Count);
            IButton btn = (IButton) buttons.Controls[3];
            Assert.AreEqual("CreateDrops", btn.Name);
        }

        [Test]
        public void Test_Layout()
        {
            //---------------Set up test pack-------------------
            const int expectedOverallWidth = 223;
            const int expectedHeight = 355;
            const int expectedBorder = 0;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ReleasesGridControl releaseSelector = new ReleasesGridControl
                                                      {Width = expectedOverallWidth, Height = expectedHeight};
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedBorder, releaseSelector.GridSelector.Left);
            Assert.AreEqual
                (expectedHeight - expectedBorder * 2, releaseSelector.GridSelector.Height);
            Assert.AreEqual(expectedOverallWidth - expectedBorder * 2, releaseSelector.GridSelector.Width);
        }

        [Test]
        public void Test_PerformClick_CallsReleaseCreateDrops()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateSavedRelease(new DateTime(2008, 3, 2), new DateTime(2008, 3, 15));
            ReleasesGridControl releaseSelector = new ReleasesGridControl();
            BusinessObjectCollection<Release> sampleCollection = Broker.GetBusinessObjectCollection<Release>("", "");
            releaseSelector.GridSelector.BusinessObjectCollection = sampleCollection;
            IButton createDropsButton = releaseSelector.Buttons["CreateDrops"];
            releaseSelector.GridSelector.SelectedBusinessObject = release;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, releaseSelector.GridSelector.BusinessObjectCollection.Count);
            Assert.IsNotNull(releaseSelector.GridSelector.SelectedBusinessObject);
            Assert.AreSame(release, releaseSelector.GridSelector.SelectedBusinessObject);
            Assert.AreEqual(0, release.Drops.Count);
            //---------------Execute Test ----------------------
            createDropsButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(2, release.Drops.Count);
        }
    }
}